﻿using Mengsk.ProxyDelivery.Domain;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using Mengsk.ProxyDelivery.App.Extenstions;
using System.Reflection;
using Mengsk.ProxyDelivery.Service;
using System.Windows.Threading;
using Mengsk.ProxyDelivery.Utils;

namespace Mengsk.ProxyDelivery.App
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        public static readonly string DIR = new FileInfo(Assembly.GetExecutingAssembly().Location).DirectoryName;
        public static readonly string DIR_CONFIG = DIR + "\\Config\\";
        public static readonly string DIR_DATA = DIR + "\\Data\\";
        public static readonly string DIR_PLUGIN = DIR + "\\Plugin\\";

        public static Operator LoginOperator { get; set; }

        public App()
        {
            Directory.CreateDirectory(DIR_CONFIG);
            Directory.CreateDirectory(DIR_DATA);
            Directory.CreateDirectory(DIR_PLUGIN);
        }

        public static void DoEvents()
        {
            Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new Action(delegate { }));
        }

        protected override void OnStartup(StartupEventArgs e)
        {
            try
            {
                AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
                Mengsk.ProxyDelivery.UI.LoginWindow lw = new ProxyDelivery.UI.LoginWindow { Title = "登录代发管理后台" };
                bool? ret = lw.ShowDialog();
                if (ret == null || ret.Value == false)
                {
                    this.Shutdown();
                    return;
                }
                LoginOperator = lw.LoginUser;
                new MainWindow3().ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误");
            }
            finally
            {
                this.Shutdown();
            }
        }

        void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            HandException(e);
        }

        private static void HandException(UnhandledExceptionEventArgs e)
        {
            try
            {
                Exception ex = e.ExceptionObject as Exception;
                string error = "";
                if (e == null)
                {
                    error = "检测到错误发生，异常信息为空";
                }
                else
                {
                    if (ex == null)
                    {
                        error = "检测到错误发生，异常信息不为excepiton:" + e.ToString();
                    }
                    else
                    {
                        error = "检测到错误发生," + ex.Message + ex.StackTrace;
                    }
                }
                MessageBox.Show(error);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "全局异常处理出错");
                string filepath = "D:\\proxydeliverylog.txt";
                System.IO.File.WriteAllText(filepath, DateTime.Now + ":" + ex.GetType().FullName + ex.Message);
            }
        }
    }
}
